package picobot.gui;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Point;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import picobot.bot.Picobot;

import picobot.interfaces.core.ISimulator;

import picobot.simulation.Simulator;

public class PicobotUserInterface extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6229227566747737816L;

	private JPanel mainPanel;
	private PicoMenuBar menuBar;
	private ISimulator simulator;
	private PicoPanel screen;
	private JScrollPane screenPanel;

	public PicobotUserInterface() {
		// setting the name to "Picobot Simulator"
		super("Picobot Simulator");

		// setting the simulator
		simulator = new Simulator();
		simulator.loadPicobot(new Picobot("default"));

		// creating and adding a PicoMenuBar
		menuBar = new PicoMenuBar();
		this.setJMenuBar(menuBar.createPicoMenuBar(this));

		// creating the main panel
		mainPanel = new JPanel();

		// seting the preferred size of the frame, the starting size
		mainPanel.setPreferredSize(new Dimension(570, 500));

		// the layout of the mainPanel is a GridLayout of size 1,1
		// note: as we have only one component, one could take any other layout
		// as well
		GridLayout grid = new GridLayout(1, 1, 0, 0);
		mainPanel.setLayout(grid);

		// JScrollPane is a panel with integrated scroll bars
		screenPanel = new JScrollPane();

		mainPanel.add(screenPanel);

		// setting the main panel to content pane of this frame
		setContentPane(mainPanel);

	}

	public ISimulator getSimulator() {
		return simulator;
	}

	public PicoPanel getScreen() {
		return screen;
	}

	public void setPicoPanel() {
		if (screen == null) {
			screen = new PicoPanel(this);
			screenPanel.getViewport().add(screen);
		} else {
			screenPanel.remove(screen);
			screen = new PicoPanel(this);
			screenPanel.getViewport().add(screen);
			screen.panelReset();
		}

		// screenAdjustment();
	}

	/**
	 * this method updates the viewport of the screenPanel so that the picobot
	 * is always in the middle it isn't used at the moment, because with small
	 * pictures the screenPanel is lagging like hell: so use it only with big
	 * pictures
	 */
	public void screenAdjustment() {
		screenPanel
				.getViewport()
				.setViewPosition(
						new Point(
								simulator.getPicobot().getXCoordinate() * 88 - 264,
								screen.getHeight()
										- (simulator.getPicobot()
												.getYCoordinate() * 46 + 230)));
	}

	/**
	 * creates the PicobotUserInterface frame and sets it to visible
	 */
	public static void createAndShowPicobotUI() {
		JFrame frame = new PicobotUserInterface();

		WindowListener l = new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		};

		
		
		// the window listener is used for better compatibility with more
		// windows
		frame.addWindowListener(l);
		frame.pack();
		frame.setVisible(true);
	}

	public static void main(String[] args) {
		// this tells the thread to run createAndShowPicobotUI, i.e. the frame
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				createAndShowPicobotUI();
			}
		});
	}

}
